from datetime import date, datetime
from horseanalysis.lib.hkjc_fetcher.fetcher_base import FetcherBase
from horseanalysis.model.race import Race
from horseanalysis.model.race_batch import RaceBatch
from horseanalysis.lib.hkjc_fetcher.win_place_fetcher import WinPlaceFetcher
from horseanalysis.lib.hkjc_fetcher.quinella_fetcher import QuinellaFetcher
from horseanalysis.lib.hkjc_fetcher.quinella_place_fetcher import QuinellaPlaceFetcher
from horseanalysis.lib.hkjc_fetcher.investment_fetcher import InvestmentFetcher
from horseanalysis.model.probability_sets.quinella_probability_result import QuinellaProbabilityResult
from horseanalysis.model.probability_sets.quinella_place_probability_result import QuinellaPlaceProbabilityResult
from horseanalysis.model.probability_sets.place_probability_result import PlaceProbabilityResult
import re

class SampleRaceFetcher:
  def __init__(self):
    if Race.all().count() == 0:
      Race.create_new_race(6, datetime(year=2009, month=10, day=7, hour=13, second=0, minute=45), 'HV', "12,4,3,11")
      Race.create_new_race(8, datetime(year=2009, month=10, day=7, hour=14, second=0, minute=50), 'HV', "11,8,4,7")
    
  def fetch_sample_race_data(self):
    for race in Race.all():
      self.race = race
      self.batch_time = RaceBatch.create_or_retrieve(self.race, datetime.now())
      for fetcher in [SampleWinPlaceFetcher, SampleQuinellaFetcher, SampleQuinellaPlaceFetcher, SampleInvestmentFetcher]:
        fetcher_obj = fetcher(self.race, self.batch_time)
        fetcher_obj.getResult()
        
      if PlaceProbabilityResult.prerequisites_checking(self.batch_time):
        for probability_result in [QuinellaProbabilityResult, QuinellaPlaceProbabilityResult, PlaceProbabilityResult]:
          probability_result.analyze_batch_if_not_existed(self.batch_time)
      

class SampleInvestmentFetcher(InvestmentFetcher):
  def getUrl(self):
    if self.race.race_no == 6:
      return "http://horseanalysis.appspot.com/sample-data/20091007_06_investment.xml"
    else:
      return "http://horseanalysis.appspot.com/sample-data/20091007_08_investment.xml"

class SampleQuinellaFetcher(QuinellaFetcher):
  def getUrl(self):
    if self.race.race_no == 6:
      return "http://horseanalysis.appspot.com/sample-data/20091007_06_quinella.xml"
    else:
      return "http://horseanalysis.appspot.com/sample-data/20091007_08_quinella.xml"

class SampleQuinellaPlaceFetcher(QuinellaPlaceFetcher):
  def getUrl(self):
    if self.race.race_no == 6:
      return "http://horseanalysis.appspot.com/sample-data/20081007_06_quinella_place.xml"
    else:
      return "http://horseanalysis.appspot.com/sample-data/20081007_08_quinella_place.xml"

class SampleWinPlaceFetcher(WinPlaceFetcher):
  def getUrl(self):
    if self.race.race_no == 6:
      return "http://horseanalysis.appspot.com/sample-data/20091007_06_win_place.xml"
    else:
      return "http://horseanalysis.appspot.com/sample-data/20091007_08_win_place.xml"